1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include<stdio.h> #include<stdlib.h> #define N 8
typedef struct node { char vexs[N]; int color[N]; int arcs[N][N];
}graph;
void DFS_direction(graph g,int i,int visited[N]) { printf("%c\n",g.vexs[i]); visited[i]=1; for(int j=0;j<N;j++) if(g.arcs[i][j]==1&&visited[j]==0) DFS_direction(g,j,visited); }
void main() { graph g; int v=0; int visited[N]={0}; int visited1[N]={0}; char vertex[N]={'A','B','C','D','E','F','G','H'}; int matrix[N][N]={{0,1,0,0,0,0,0,0}, {0,0,1,0,0,1,0,0}, {0,0,0,1,0,0,1,0}, {0,0,1,0,0,0,0,1}, {1,0,0,0,0,1,0,0}, {0,0,0,0,0,0,1,0}, {0,0,0,0,0,1,0,1}, {0,0,0,0,0,0,0,1}}; for(int i=0;i<N;i++) { g.vexs[i]=vertex[i]; for(int j=0;j<N;j++) g.arcs[i][j]=matrix[i][j]; } int d[N]={0}; int f[N]={0}; int num=0; while(num!=N) { for(int j=0;j<N;j++) if(visited[j]==0) DFS_direction(g,j,visited); for(int k=0;k<N;k++) num=num+visited[k]; }
}
|